$(function () {
    findById();
});

function findById() {
    let id = sessionStorage.getItem("userId");
    $.ajax({
        url: '/back/user/findById',
        data: {id: id},
        type: 'get',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            setData(data.model);
        }
    })
}

function setData(data) {
    layui.use(['form'],
        function () {
            $ = layui.jquery;
            var form = layui.form;

            $("#username").val(data.username);
            $("#phone").val(data.phone);
            $("#birthday").val(data.birthday);
            let hobby = data.hobby;
            let hobbys = hobby.split(",");
            $('input[type=checkbox][name=hobby]').each(function () {
                // arr_box.push($(this).val());
                for (let i = 0; i < hobbys.length; i++) {

                    if ($(this).val() == hobbys[i]) {
                        console.log($(this).val(), hobbys[i]);
                        $(this).attr("checked", "true");
                    }
                }

            });
            $("input[name=sex][value=" + data.sex + "]").attr("checked", true);
            //刷新表单
            form.render();


        });


}

layui.use(['form', 'layer', 'jquery', 'laydate'],
    function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;
        var laydate = layui.laydate;
        //自定义验证规则
        form.verify({
            nikename: function (value) {
                if (value.length < 5) {
                    return '昵称至少得5个字符啊';
                }
            },
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });
        //执行一个laydate实例
        laydate.render({
            elem: '#birthday' //指定元素
            , trigger: 'click'
        });
        //监听提交
        form.on('submit(update)',
            function (data) {
                console.log(data);
                data = data.field;

                var arr_box = [];
                $('input[type=checkbox][name=hobby]:checked').each(function () {
                    arr_box.push($(this).val());
                });
                data.hobby = arr_box.toLocaleString();
                data.id=sessionStorage.getItem("userId");
                //数组
                console.log(data);
                let result=myAjax("/back/user/update",data,"post");
                if (result.result == "success") {
                    layer.alert("编辑成功", {
                            icon: 6
                        },
                        function () {
                            //关闭当前frame
                            xadmin.close();

                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        });
                } else {
                    layer.alert("编辑失败");
                }

                // $.ajax({
                //     url: '/back/user/update',
                //     data: data,
                //     type: 'post',
                //     dataType: 'json',
                //     success: function (data) {
                //         //发异步，把数据提交给php
                //
                //     }
                // });

                return false;
            });

    });
